package com.yenlien.traceyun.plant.service;

import com.yenlien.traceyun.business.dao.DetectionDao;
import com.yenlien.traceyun.business.dao.ProcessDetailDao;
import com.yenlien.traceyun.business.dao.StorageManageDao;
import com.yenlien.traceyun.business.entity.Detection;
import com.yenlien.traceyun.business.entity.ProcessDetail;
import com.yenlien.traceyun.business.entity.StorageManage;
import com.yenlien.traceyun.business.service.rest.usercenter.FileRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class DetectionService {
    @Autowired
    private DetectionDao detectionDao;
    @Autowired
    private ProcessDetailDao processDetailDao;
    @Autowired
    private StorageManageDao storageManageDao;
    @Autowired
    private FileRestService fileService;

    @Transactional
    public void save(Detection detection, List<MultipartFile> files) throws Exception {
        fileService.upload(null, detection, files);
        detection.setLastStep(detection.getRelevanceId());
        detectionDao.save(detection);

        String type = detection.getType();
        if (("采购检测").equals(type)) {
            StorageManage storageManage = storageManageDao.findById(detection.getRelevanceId());
            storageManage.setIsCheck(1);
            storageManageDao.save(storageManage);
        } else if (("加工检测").equals(type)) {
            ProcessDetail processDetail = processDetailDao.findById(detection.getRelevanceId());
            processDetail.setIsCheck(1);
            processDetailDao.save(processDetail);
        }
    }

    public Detection findById(String id) {
        return detectionDao.findById(id);
    }

    public List<Detection> findByRelevanceId(String relevanceId) {
        return detectionDao.findByRelevanceId(relevanceId);
    }
}
